/**
 * 2017年5月3日
 */
package cn.edu.bjtu.entity.impl.local;

import java.io.File;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.ReadWriteLock;

import cn.edu.bjtu.entity.LibSVMHolderContext;
import cn.edu.bjtu.entity.impl.BaseLocalContext;


/**
 * @author Alex
 *
 */
public class LibSVMExecuteContext extends BaseLocalContext<LibSVMHolderContext>{

	private final LibSVMHolderContext holder;
	
	/**
	 * @param lock
	 * @param inputDir
	 * @param outputDir
	 */
	public LibSVMExecuteContext(ReadWriteLock lock, String inputDir, String outputDir,LibSVMHolderContext ctx) {
		super(lock, inputDir, outputDir);
		this.holder = ctx;
	}
	
	Map<String,Object> m = new TreeMap<String, Object>();
	String targetName = "sample_libsvm_data.txt";
	String outputName = "output.txt";
	
	
	/**
	 * @param targetName the targetName to set
	 */
	public void setTargetName(String targetName) {
		if(targetName != null)
			this.targetName = targetName;
	}

	@Override
	public Map<String, Object> getResultMapForRender() {
		// TODO Auto-generated method stub
		return m;
	}

	@Override
	public void fireInternal() throws Exception{
		if(this.lock.writeLock().tryLock()){
			try{
				File f = new File(getInputDir(),targetName);
				File o = new File(getOutPutDir(),outputName);
				holder.setIn(f.getAbsolutePath());
				holder.setOut(o.getAbsolutePath());
				holder.train();
			}finally{
				this.lock.writeLock().unlock();
				this.msg = "Build Success" ;
				m.put("tip", this.holder.toString());
			}
		}else{
			executing = true;
			failed = true;
			msg = "executing";
		}
		
	}
	
	@Override
	public LibSVMHolderContext javaClassResult() {
		return holder;
	}

}
